﻿#include "Player.h"
#include "Spell.h"
#include "Item.h"
#include "Map.h"
#include "SimpleAudioEngine.h"
#pragma execution_character_set("utf-8")

Player::Player() : Unit(TypeID_Player)
{
	initWithFile("action/player/stand/stand1.png");
	autorelease();
	NeedUpdatePlayerValue = false;
	for (int action = STAND; action != ATTACK_CIRT; action++)
	{
		_PlayerDoingActions[(PlayerAction)action] = false;
		_PlayerActions[(PlayerAction)action] = nullptr;
	}
	m_ActionTimer = 0;
	m_DisableFall = false;
	m_CanDoAction = true;
	_Jumper = nullptr;
	m_IsJumping = false;
	m_AttackTimer = 0;
	FallingSpeed = 1.0f;
	m_CanDeal_Crash_Damage = true;
	m_Crash_Damage_Timer = 1.0f;
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->preloadEffect("sound/action/jump.wav");
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->preloadEffect("sound/action/Attack1.wav");
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->preloadEffect("sound/action/Die.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->preloadEffect("sound/action/PickUpItem.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->preloadEffect("sound/action/LevelUp.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->preloadEffect("sound/action/Tombstone.mp3");
	LoadFromDB();
	//InitBag(); 修改至Map_Layer::init()
	m_SaveToDBDelay = 60.0f;
	scheduleUpdate();
}
Player::~Player()
{
	SaveToDB();
}

void Player::RemoveItemFromSlot(Slot* pSlot)
{
	pSlot->GetItem()->DeleteFromDB();
	pSlot->ReSetSlotItem();
	pSlot->removeAllChildren();
}

bool Player::CanEquipItem(int _slot, Item* pItem)
{
	if (pItem->GetRequireClass() != GetClass())
	{
		SendNotify("职业不符!");
		return false;
	}
	if (pItem->GetNeedLevel() > GetLevel())
	{
		SendNotify("没有达到需求等级!");
		return false;
	}
	switch (EquipBag->GetSlot(_slot)->getTag())
	{
	case SLOT_RING_ZERO:
	case SLOT_RING_ONE:
	case SLOT_RING_TWO:
	case SLOT_RING_THR:
	case SLOT_RING_FOR:
	case SLOT_RING_FIVE:
		if (pItem->GetEquipItemSlot() == RING_ZERO)
			return true;
	case SLOT_NECKLACE_ZERO:
	case SLOT_NECKLACE_ONE:
		if (pItem->GetEquipItemSlot() == NECKLACE_ZERO)
			return true;
	case SLOT_MAIN_HAND:
	case SLOT_BELT:
	case SLOT_HEAD:
	case SLOT_FACE:
	case SLOT_EYE:
	case SLOT_JACKET:
	case SLOT_PANTS:
	case SLOT_SHOES:
	case SLOT_PACKET:
	case SLOT_EAR:
	case SLOT_SHOULDER:
	case SLOT_WRIST:
	case SLOT_ROBOT:
	case SLOT_SOURCE:
	case SLOT_DADGE_ZERO:
	case SLOT_DADGE_ONE:
	case SLOT_OFF_HAND:
	case SLOT_BACK:
	case SLOT_ROBOT_HEART:
		if (pItem->GetEquipItemSlot() == EquipBag->GetSlot(_slot)->getTag())
			return true;
	}
	SendNotify("装备位置不符合!");
	return false;
}

void Player::SendNotify(std::string _noti)				
{ 
	PlayerUILayer::GetInstance()->SendPlayerNotify(_noti); 
}

void Player::GetExpReward(int _value)
{
	if (GetInt32Value(EXP) + _value >= GetNextLevelExp())
	{
		int OutRangeExp = 0;
		OutRangeExp = GetInt32Value(EXP) + _value - GetNextLevelExp();
		PlayerLevelUp();
		if (OutRangeExp)
			GetExpReward(_value);
	}
	else if (_value > 0)
	{
		m_Player_Int32Values[EXP] += _value;
	}
}

void Player::PlayerLevelUp(int _Level)
{
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->playEffect("sound/action/LevelUp.mp3");
	m_Player_Int32Values[VALUE_LEVEL]++;
	PlayerUILayer::GetInstance()->ResetLevelString();
	//HP MP EXP
	m_Player_Int32Values[EXP] = 0;
	m_Player_Int32Values[VALUE_HP] = CalcPlayerTotalValue(Item_Value_Heath);
	m_Player_Int32Values[VALUE_MP] = CalcPlayerTotalValue(Item_Value_Mana);
}

const std::string Player::GetClassString()
{
	std::string classname = "未知";
	switch (GetClass())
	{
	case BASE_NONE:
		classname = "新手";
		break;
	case BASE_WARRIOR:
		classname = "战士";
		break;
	case BASE_MAGIC:
		classname = "法师";
		break;
	case BASE_THIEF:
		classname = "盗贼";
		break;
	case BASE_ARCHER:
		classname = "弓手";
		break;
	}
	return classname;
}

void Player::Revive()
{
	SetHeath(GetInt32Value(VALUE_MAXHP));
	SetDeadStatus(NotDead);
	setVisible(true);
	SaveToDB();
	m_SaveToDBDelay = 60.0f;
	scheduleUpdate();
}

void Player::AddItem(int Item_Entry, int _Count)
{
	if (int pSlot = PlayerBag->GetEmptySlot())
	{
		Item* pItem = new Item(Item_Entry, this);
		pItem->SetSlot(pSlot);
		if (Slot* _Slot = PlayerBag->GetSlot(pSlot))
		{
			_Slot->SetItemGuid(pItem->GetGuid());
			_Slot->SetItem(pItem);
			pItem->setTag(_Slot->getTag() + 100);
			pItem->setPosition(_Slot->getContentSize().width / 2, _Slot->getContentSize().height / 2);
			_Slot->addChild(pItem);
			PlayerUILayer::GetInstance()->AddLootInfoToLayer(pItem->GetName());
		}
	}
}

void Player::GetPunish()
{
	if (sWorld->GetDeadPunish_Exp())
	{
		if (GetInt32Value(EXP) * sWorld->GetDeadPunish_Exp() > 0)
			SetInt32Value(EXP, GetInt32Value(EXP) - (GetInt32Value(EXP) * sWorld->GetDeadPunish_Exp()));
	}
	if (sWorld->GetDeadPunish_Money())
	{
		if (GetMoney() >= sWorld->GetDeadPunish_Money())
			ModifyMoney(0 - sWorld->GetDeadPunish_Money());
	}
}

void Player::PlayerDead()
{
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->playEffect("sound/action/Tombstone.mp3");
	setVisible(false);
	CCSprite* DeadSprite = new CCSprite();
	DeadSprite->initWithFile("action/player/dead/dead (1).png");
	DeadSprite->autorelease();
	DeadSprite->setPosition(getPositionX(), getPositionY() + 400);
	CCMoveTo* DeadSpriteMove = CCMoveTo::create(1.0f, ccp(getPositionX(), getPositionY() - getBoundingBox().size.height / 2 + DeadSprite->getBoundingBox().size.height / 2));
	DeadSprite->runAction(DeadSpriteMove);
	CCAnimation* Deadaction = new CCAnimation();
	for (size_t i = 0; i < 12; i++)
	{
		char msg[255];
		snprintf(msg, 255, "action/player/dead/dead (%d).png", i + 1);
		Deadaction->addSpriteFrameWithFile(msg);
	}
	Deadaction->autorelease();
	Deadaction->setDelayPerUnit(1.0f / 12.0f);
	Deadaction->setRestoreOriginalFrame(true);
	Deadaction->setLoops(1);
	CCAnimate* walkanimate = CCAnimate::create(Deadaction);
	CCSequence* spawn = CCSequence::create(walkanimate, CallFunc::create(CC_CALLBACK_0(Player::CallDeadTalkClass, this)), NULL);
	DeadSprite->runAction(spawn);
	Map_Layer::GetInstance()->addChild(DeadSprite);
	GetPunish();
}

void Player::CallDeadTalkClass()
{
	PlayerUILayer::GetInstance()->getChildByTag(PLAYER_DEAD_TALK_CLASS_TAG)->setVisible(true);
}

void Player::AddItem(Item* pItem, int _Count)
{

}

int Player::GetNextLevelExp()
{
	if (GetLevel() < 1 || GetLevel() > 249)
		return -1;
	return Map_Scene::GetInstance()->GetLevelInfo().at(GetLevel() - 1).NeedExp;
}

CCLabelTTF* Player::GetItemViewTTF(ItemTextType _ItemTextType)
{
	if (ItemViewTexts_MainViewWindow.find(_ItemTextType) != ItemViewTexts_MainViewWindow.end())
	{
		return ItemViewTexts_MainViewWindow[_ItemTextType];
	}
	return nullptr;
}

void Player::InitBag()
{
	PlayerBag = new Bag();
	PlayerBag->setPosition(Director::sharedDirector()->getVisibleSize().width / 2, Director::sharedDirector()->getVisibleSize().height / 2);
	PlayerBag->setTag(BAG_TAG);
	PlayerUILayer::GetInstance()->addChild(PlayerBag);
	PlayerBag->setVisible(false);
	PlayerBag->SetOwner(this);
	PlayerBag->LoadBagFromDB();


	ItemViewWindow_Main = CCSprite::create("ui/ItemViewWindow.png");
	ItemViewWindow_Main->setPosition(PlayerBag->getBoundingBox().origin.x - ItemViewWindow_Main->getBoundingBox().size.width / 2 - ItemViewWindow_Main->getBoundingBox().size.width, PlayerBag->getBoundingBox().origin.y + ItemViewWindow_Main->getBoundingBox().size.height / 2);
	ItemViewWindow_Main->setTag(ITEM_VIEW_MAIN_TAG);
	ItemViewWindow_Main->setOpacity(255*0.75f);
	ItemViewWindow_Main->setVisible(false);
	PlayerUILayer::GetInstance()->addChild(ItemViewWindow_Main);

	ItemView_BigImage = CCSprite::create("ui/ItemView_BigImage.png");
	ItemView_BigImage->setPosition(ItemViewWindow_Main->getContentSize().width * 0.05 + ItemView_BigImage->getBoundingBox().size.width / 2, ItemViewWindow_Main->getContentSize().height / 1.3);
	ItemView_BigImage->setTag(ITEM_VIEW_MAIN_IMAGE);
	ItemViewWindow_Main->addChild(ItemView_BigImage);
	CCSprite* sub = CCSprite::create("ui/ItemView_BigImage.png");
	sub->setPosition(ItemView_BigImage->getContentSize().width / 2, ItemView_BigImage->getContentSize().height / 2);
	sub->setTag(ITEM_VIEW_MAIN_IMAGE_SUB_VIEW);
	ItemView_BigImage->addChild(sub);

	for (int i = NAME_TITLE; i != ITEM_TEXTTYPE_END; i++)
	{
		ItemViewTexts_MainViewWindow[(ItemTextType)i] = new CCLabelTTF();
		//缺少Item* GetItem() 待填充
		//ItemViewTexts_MainViewWindow[(ItemTextType)i]->create("");
		float SinglePointXModify = ItemViewWindow_Main->getBoundingBox().size.width / 6;;
		switch ((ItemTextType)i)
		{
			case NAME_TITLE:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("测试武器标题", "Airal", 16, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewWindow_Main->getContentSize().width / 2, ItemViewWindow_Main->getContentSize().height - (ItemViewTexts_MainViewWindow[(ItemTextType)i]->getBoundingBox().size.height / 1.5));
				break;
			case BONDING_TYPE:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("交换选项", "Airal", 12, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewWindow_Main->getContentSize().width / 2, ItemViewTexts_MainViewWindow[NAME_TITLE]->getBoundingBox().origin.y - (ItemViewTexts_MainViewWindow[(ItemTextType)i]->getBoundingBox().size.height / 1.5));
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setColor(ccc3(255, 165, 0));
				break;
			case ATTACK_MODIFY:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("攻击力提升", "Airal", 12, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewWindow_Main->getContentSize().width - ItemViewTexts_MainViewWindow[(ItemTextType)i]->getBoundingBox().size.width * 0.6, ItemViewWindow_Main->getContentSize().height / 1.2);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setColor(ccc3(211, 211, 211));
				break;
			case ATTACK_MODIFY_NUMBER:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("+1999999", "Airal", 17, Size::ZERO, TextHAlignment::RIGHT);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewWindow_Main->getContentSize().width - ItemViewTexts_MainViewWindow[(ItemTextType)i]->getBoundingBox().size.width * 0.6, ItemViewWindow_Main->getContentSize().height / 1.3);
				break;
			case REQUIRE_LEVEL:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("·REQ LEV:118", "Airal", 11, Size::ZERO, TextHAlignment::LEFT);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemView_BigImage->getBoundingBox().origin.x + ItemView_BigImage->getBoundingBox().size.width + ItemView_BigImage->getBoundingBox().size.width * 0.65, ItemViewWindow_Main->getContentSize().height / 1.4);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setColor(ccc3(255, 215, 0));
				break;
			case REQUIRECLASS_GREEN_HAND:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("新手", "Airal", 12, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewTexts_MainViewWindow[(ItemTextType)i]->getBoundingBox().size.width * 0.7, ItemViewWindow_Main->getBoundingBox().size.height / 1.65);
				break;
			case REQUIRECLASS_WORRIOR:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("战士", "Airal", 12, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewTexts_MainViewWindow[(ItemTextType)(i - 1)]->getPositionX() + SinglePointXModify, ItemViewWindow_Main->getBoundingBox().size.height / 1.65);
				break;
			case REQUIRECLASS_MAGIC:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("魔法师", "Airal", 12, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewTexts_MainViewWindow[(ItemTextType)(i - 1)]->getPositionX() + SinglePointXModify, ItemViewWindow_Main->getBoundingBox().size.height / 1.65);
				break;
			case REQUIRECLASS_ACR:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("弓手", "Airal", 12, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewTexts_MainViewWindow[(ItemTextType)(i - 1)]->getPositionX() + SinglePointXModify, ItemViewWindow_Main->getBoundingBox().size.height / 1.65);
				break;
			case REQUIRECLASS_THEIF:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("盗贼", "Airal", 12, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewTexts_MainViewWindow[(ItemTextType)(i - 1)]->getPositionX() + SinglePointXModify, ItemViewWindow_Main->getBoundingBox().size.height / 1.65);
				break;
			case REQUIRECLASS_PIRATE:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("海盗", "Airal", 12, Size::ZERO, TextHAlignment::CENTER);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewTexts_MainViewWindow[(ItemTextType)(i - 1)]->getPositionX() + SinglePointXModify, ItemViewWindow_Main->getBoundingBox().size.height / 1.65);
				break;
			case ITEM_VALUES:
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->initWithString("装备分类:测试\n力量:123\n敏捷:123\n智力\n运气\nMaxHP\nMaxMP\n防御力\n魔法防御力\n命中值\n回避值", "Airal", 14, Size::ZERO, TextHAlignment::LEFT, TextVAlignment::TOP);
				ItemViewTexts_MainViewWindow[(ItemTextType)i]->setPosition(ItemViewTexts_MainViewWindow[(ItemTextType)i]->getBoundingBox().size.width / 1.4, ItemViewWindow_Main->getBoundingBox().size.height / 3.5);
				break;
		}
		ItemViewTexts_MainViewWindow[(ItemTextType)i]->autorelease();
		ItemViewWindow_Main->addChild(ItemViewTexts_MainViewWindow[(ItemTextType)i]);
	}

	ItemViewWindow_Sult = CCSprite::create("ui/ItemViewWindow.png");
	ItemViewWindow_Sult->setScaleY(0.4f);
	ItemViewWindow_Sult->setPosition(PlayerBag->getBoundingBox().origin.x - ItemViewWindow_Main->getBoundingBox().size.width / 2 - ItemViewWindow_Main->getBoundingBox().size.width * 0.005, PlayerBag->getBoundingBox().origin.y + PlayerBag->getBoundingBox().size.height - ItemViewWindow_Sult->getBoundingBox().size.height / 2);
	ItemViewWindow_Sult->setTag(ITEM_VIEW_SULT_TAG);
	ItemViewWindow_Sult->setOpacity(255 * 0.75f);
	ItemViewWindow_Sult->setVisible(false);
	PlayerUILayer::GetInstance()->addChild(ItemViewWindow_Sult);

	ItemViewWindow_SultView = CCSprite::create("ui/ItemViewWindow.png");
	ItemViewWindow_SultView->setScaleY(0.6f);
	ItemViewWindow_SultView->setPosition(PlayerBag->getBoundingBox().origin.x - ItemViewWindow_Main->getBoundingBox().size.width / 2 - ItemViewWindow_Main->getBoundingBox().size.width * 0.005, ItemViewWindow_Sult->getBoundingBox().origin.y - ItemViewWindow_SultView->getBoundingBox().size.height / 2);
	ItemViewWindow_SultView->setTag(ITEM_VIEW_SULT_VIEW_TAG);
	ItemViewWindow_SultView->setOpacity(255 * 0.75f);
	ItemViewWindow_SultView->setVisible(false);
	PlayerUILayer::GetInstance()->addChild(ItemViewWindow_SultView);
}

void Player::SaveToDB()
{
	//save
	char msg[255];
	//											1			2			3		4				5			6					7			8				9				10				11				12				13					14	15
	snprintf(msg, 255, "UPDATE characters SET map_id = %d,money = %d,jf = %d,value_str = %d,value_dex = %d,value_int = %d,value_luck = %d,value_hp = %d,value_maxhp = %d,value_mp = %d,value_maxmp = %d,value_level = %d,value_exp = %d,class = %d,talent = %d WHERE player_guid = %d",
		//1				2				3								4							5										6							7							8				
		GetMapId(), GetMoney(), m_Player_Int32Values[JF], m_Player_Int32Values[VALUE_STR], m_Player_Int32Values[VALUE_DEX], m_Player_Int32Values[VALUE_INT], m_Player_Int32Values[VALUE_LUCK], m_Player_Int32Values[VALUE_HP]
		//			9									10								11								12									13							14						15							16
		, m_Player_Int32Values[VALUE_MAXHP], m_Player_Int32Values[VALUE_MP], m_Player_Int32Values[VALUE_MAXMP], m_Player_Int32Values[VALUE_LEVEL], m_Player_Int32Values[EXP], m_Player_Int32Values[CLASS], m_Player_Int32Values[TALENT], GetGuid());
	Database::GetInstance()->PExcude(CharacterDatabase, msg);
		
		char msa[255];
		snprintf(msa, 255, "UPDATE characters SET unused_talent = %d,move_speed = %d,jump_high = %d,player_name = '%s',pos_x = %f ,pos_y = %f WHERE player_guid = %d", m_Player_Int32Values[UNUSE_TALENT], m_Player_Int32Values[MOVE_SPEED],
		m_Player_Int32Values[JUMP_HIGH], GetName().c_str(), getPositionX(), getPositionY(), GetGuid());
		Database::GetInstance()->PExcude(CharacterDatabase, msa);
}

void Player::LoadFromDB()
{
	Result::Qurey result = Database::GetInstance()->GetResult(CharacterDatabase,
		//			0			1	2	  3		4		5			6		7			8			9		10			11		12			13			14	15		16				17			18		19			20	21			
		"SELECT player_guid,map_id,money,jf,value_str,value_dex,value_int,value_luck,value_hp,value_maxhp,value_mp,value_maxmp,value_level,value_exp,class,talent,unused_talent,move_speed,jump_high,player_name,pos_x,pos_y FROM characters;");

	if (!result.empty())
	{
		int ValueName = UNIT_GUID;
		Result::Qurey::iterator itr = result.begin();
		for (int i = UNIT_GUID; i != JUMP_HIGH + 1; i++)
		{
			if (!i)
				SetGuid(atoi(itr->second.at(i).c_str()));
			m_Player_Int32Values[(Int32Value)i] = atoi(itr->second.at(i).c_str());
		}
		SetName(itr->second.at(19).c_str());
		setPositionX(atof(itr->second.at(20).c_str()));
		setPositionY(atof(itr->second.at(21).c_str()));
	}
}

void Player::Jump(JumpFacing Left)
{
	_Jumper = new Jumper();
	CocosDenshion::SimpleAudioEngine::getInstance()->sharedEngine()->playEffect("sound/action/jump.wav");
	_Jumper->JumpFacing = Left;
	_Jumper->JumpStartPointX = getPositionX();
	_Jumper->JumpStartPointY = getPositionY();
	BaseJumpHigh = _Jumper->JumpHigh;
	m_IsJumping = true;
}

CCAnimate* Player::GetAction(PlayerAction Action)
{
	switch (Action)
	{
	case STAND:
	{
		//Stand
		CCAnimation* standanimation = new CCAnimation();
		for (int i = 1; i != 4; i++)
		{
			char str[200];
			sprintf(str, "action/player/stand/stand%d.png", i);
			standanimation->addSpriteFrameWithFile(str);
		}
		standanimation->setDelayPerUnit(1.0f / 4.0f);
		standanimation->setRestoreOriginalFrame(true);
		standanimation->setLoops(1);
		standanimation->autorelease();
		CCAnimate* standanimate = CCAnimate::create(standanimation);
		return standanimate;
		break;
	}
	case WALK:
	{
		//walk
		CCAnimation* walkanimation = new CCAnimation();
		for (int i = 1; i != 5; i++)
		{
			 char str[200];
			 sprintf(str, "action/player/walk/walk%d.png", i);
			 walkanimation->addSpriteFrameWithFile(str);
		}
		walkanimation->setDelayPerUnit(1.0f / 5.0f);
		walkanimation->setRestoreOriginalFrame(true);
		walkanimation->setLoops(1);
		walkanimation->autorelease();
		CCAnimate* walkanimate = CCAnimate::create(walkanimation);
		return walkanimate;
		break;
	}
	case DOWN:
		break;
	case JUMP:
	{
		//Jump
		CCAnimation* walkanimation = new CCAnimation();
		walkanimation->addSpriteFrameWithFile("action/player/jump/jump.png");
		walkanimation->setDelayPerUnit(0.4f / 1.0f);
		walkanimation->setRestoreOriginalFrame(true);
		walkanimation->setLoops(1);
		walkanimation->autorelease();
		CCAnimate* walkanimate = CCAnimate::create(walkanimation);
		return walkanimate;
		break;
	}
	case ATTACK_NORMAL:
	{
		//attack normal
		CCAnimation* walkanimation = new CCAnimation();
		for(int i = 1; i != 9; i++)
		{
			char str[200];
			sprintf(str, "action/player/attack/normal/normal1_%d.png", i);
			walkanimation->addSpriteFrameWithFile(str);
		}
		walkanimation->setDelayPerUnit(0.5f / 9.0f);
		walkanimation->setRestoreOriginalFrame(true);
		walkanimation->setLoops(1);
		walkanimation->autorelease();
		CCAnimate* walkanimate = CCAnimate::create(walkanimation);
		return walkanimate;
		break;
	}
	case ATTACK_CIRT:
		break;
	default:
		break;
	}
}

void Player::ResetSprite()
{
	runAction(GetAction(STAND));
}

int Player::CalcPlayerTotalValue(int _ItemValue)
{
	int Total = 0;
	switch (_ItemValue)
	{
	case Item_Value_Mana:
		Total += GetInt32Value(VALUE_MAXMP);
		break;
	case Item_Value_Heath:
		Total += GetInt32Value(VALUE_MAXHP);
		break;
	case Item_Value_Str:
		Total += GetInt32Value(VALUE_STR);
		break;
	case Item_Value_Dex:
		Total += GetInt32Value(VALUE_DEX);
		break;
	case Item_Value_Int:
		Total += GetInt32Value(VALUE_INT);
		break;
	case Item_Value_Luk:
		Total += GetInt32Value(VALUE_LUCK);
		break;
	case Item_Value_JumpHigh:
		Total += GetInt32Value(MOVE_SPEED);
		break;
	case Item_Value_MoveSpeed:
		Total += GetInt32Value(JUMP_HIGH);
		break;
	}
	for (int i = PLAYER_EQUIP_SLOT_START; i != PLAYER_EQUIP_SLOT_END; i++)
	{
		if (!GetEquipBag()->GetSlot(i)->IsEmptySlot())
		{
			Item* CalcItem = GetEquipBag()->GetSlot(i)->GetItem();
			if (!CalcItem)
			{
				SendNotify("Nullptr Item At Player::CalcPlayerValues()!");
			}
			else
			{
				Total += CalcItem->GetItemBaseValue((ItemValue)_ItemValue);
			}
		}
	}
	if (_ItemValue == Item_Value_Att)
	{
		int str = Map_Scene::GetInstance()->GetLoadedPlayer()->GetTotalValueNumber(Item_Value_Str);
		int dex = Map_Scene::GetInstance()->GetLoadedPlayer()->GetTotalValueNumber(Item_Value_Dex);
		int Int = Map_Scene::GetInstance()->GetLoadedPlayer()->GetTotalValueNumber(Item_Value_Int);
		int Luk = Map_Scene::GetInstance()->GetLoadedPlayer()->GetTotalValueNumber(Item_Value_Luk);
		Total = Total * (4 * Luk + 1 * dex) * 1.25;
	}
	return Total;
}

int Player::GetTotalValueNumber(int _ItemValue)
{ 
	return CalcPlayerTotalValue(_ItemValue); 
}

void Player::DoAction(PlayerAction Action, bool Active)
{
	if (Action == JUMP && m_CanDoAction)
	{
		stopAllActions();
		runAction(GetAction(Action));
		m_CanDoAction = false;
		m_ActionTimer = 0.4f;
		return;
	}
	if (Action == ATTACK_NORMAL)
	{
	}
	if (m_CanDoAction)
	if (!_PlayerDoingActions[Action])
	{
		runAction(GetAction(Action));
		m_CanDoAction = false;
		m_ActionTimer = 1.0f;
	}
}

void Player::UpdatePlayerValue()
{ 
	PlayerUILayer::GetInstance()->ReLoadPlayerValueNumber(); 
}

void Player::update(float diff)
{
	Unit::update(diff);

	if (m_SaveToDBDelay <= diff)
	{
		SaveToDB();
		m_SaveToDBDelay = 60.0f;
	}
	else m_SaveToDBDelay -= diff;

	if (m_ActionTimer <= diff)
	{
		m_CanDoAction = true;
	}
	m_ActionTimer -= diff;

	if (_Jumper)
	{
		if (!m_DisableFall)
			m_DisableFall = true;
		if (_Jumper->JumpHigh > 0)
		{
			float TempJumpHigh = (diff / 0.2) * BaseJumpHigh;
			setPositionY(getPositionY() + TempJumpHigh);
			_Jumper->JumpHigh -= TempJumpHigh;
		}
		else
		{
			delete _Jumper;
			_Jumper = nullptr;
			m_DisableFall = false;
			m_IsJumping = false;
		}
	}

	if (NeedUpdatePlayerValue)
	{
		UpdatePlayerValue();
		NeedUpdatePlayerValue = false;
	}

	if (!m_CanDeal_Crash_Damage)
	{
		if (m_Crash_Damage_Timer <= diff)
		{
			m_Crash_Damage_Timer = 1.0f;
			m_CanDeal_Crash_Damage = true;
		}
		else m_Crash_Damage_Timer -= diff;
	}
}